package org.meandslava.calc.arithmetic;

import java.util.LinkedList;

public class Binary extends Arithmetic {
	public Binary(String tag){
		super(tag);
		//myCategoryTag = CATEGORY_BINARY;
	}
	
	@Override
	public double calc(double previousValue, LinkedList<Term> queue) {
		if(queue.size() == 0){
			return previousValue;
		}
		
		if(queue.peek() instanceof Number){
			Number secondArg = (Number)queue.poll();
			if(tag.equals("+")){
				return previousValue + secondArg.getValue();				
			}else if(tag.equals("-")){
				return previousValue - secondArg.getValue();
			}else if(tag.equals("*")){
				return previousValue * secondArg.getValue();
			}else if(tag.equals("/")){
				return previousValue / secondArg.getValue();
			}else if(tag.equals("-")){
				return previousValue - secondArg.getValue();
			}
			return previousValue;
		}

		throw new IllegalArgumentException("The object is not a number instance: " + String.valueOf(queue.peek()));
	}

	@Override
	public boolean process(String categoryTag, String action) {
		if(categoryTag.equals(CATEGORY_BINARY)){
			this.tag = action;
			return true;
		}
		return false;
	}

}
